#include <stdio.h>
#include "h/pe12-2a.h"

/*static Mode mode;
static double distance;
static double fuel;
*/

static void get_info(Mode);
static void show_info(Mode, double, double);

void set_mode(int m)
{
    if (m != METRIC && m != US) {
        printf("Invalid mode specified. Mode 1(US) used.\n");
        m = US;
    }

    //mode = m;
    get_info(m);
}

static void get_info(Mode mode)
{
    double distance;
    double fuel;
    switch (mode) {
        case METRIC:
            printf("Enter distance traveled in kilometers: ");
            if (scanf("%lf", &distance) !=1)
                printf("Enter is invalid, try again!\n");

            printf("Enter fuel consumed in liters: ");
            if (scanf("%lf", &fuel) !=1)
                printf("Enter is invalid, try again!\n");
            break;

        case US:
            printf("Enter distance traveled in miles: ");
            if (scanf("%lf", &distance) !=1)
                printf("Enter is invalid, try again!\n");

            printf("Enter fuel consumed in gallons: ");
            if (scanf("%lf", &fuel) != 1)
                printf("Enter is invalid, try again!\n");
    }

    show_info(mode, distance, fuel);
}

static void show_info(Mode mode, double distance, double fuel)
//void show_info(void)
{
    switch (mode) {
        case METRIC:
            printf("Fuel consumption is %.2lf liters per 100km.\n", fuel / distance * 100);
            break;

        case US:
            printf("Fuel consumption is %.1lf miles per gallon.\n", distance / fuel);
            break;

    }
}
